복합 타입의 구조
C++ 복합 타입 기본 타입과 타입 수정자. 참조와 포인터 모두 간접 참조를 제공하지만 철학적으로는 서로 다릅니다. 참조 (&) 는 영구적인 별칭—기존 객체에 대한 별명입니다. 한 번 바인딩되면 재지정할 수 없습니다. 반면, 포인터 (*) 는 메모리 내에서 독립된 객체로 16진수 형태의 주소를 저장합니다. 다른 객체로 재지정하거나 nullptr로 설정할 수 있습니다.
메모리 시각화
코드에서 int *p1, p2;에서는 p1 포인터이며, p2 는 단순한 정수입니다. 둘 다 포인터로 만들려면 int *p1, *p2;를 사용해야 합니다. 이는 수정자(예: *)가 기본 타입이 아니라 개별 선언자에 속한다는 점을 강조합니다.
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which of the following creates a reference correctly?
int &r;
int i = 10; int &r = i;
int &r = 10;
int *&r = &i;
✅ Correct!
Correct! References must be initialized and bound to an object, not a literal or nothing.❌ Incorrect
References are not objects; they must be bound to an existing variable upon declaration.QUESTION 2
In the declaration 'int* p1, p2;', what are the types of p1 and p2?
Both are pointers to int.
p1 is an int, p2 is a pointer to int.
p1 is a pointer to int, p2 is a plain int.
Neither are valid C++.
✅ Correct!
The '*' modifier applies only to p1. The base type is int, but the declarator p2 lacks the modifier.❌ Incorrect
Type modifiers like '*' or '&' attach to the specific variable name, not the base type (int).QUESTION 3
Which operator is used to obtain the memory address of an object?
The asterisk (*)
The ampersand (&)
The arrow (->)
The dot (.)
✅ Correct!
The address-of operator (&) returns the location of an object in memory.❌ Incorrect
The asterisk (*) is used for dereferencing (accessing the value at an address) or declaration.QUESTION 4
What is the result of dereferencing a pointer?
It gives the memory address.
It creates a new reference.
It yields the object to which the pointer points.
It clears the pointer's memory.
✅ Correct!
Dereferencing 'follows' the address to the actual object, allowing you to read or modify it.❌ Incorrect
Dereferencing retrieves the value, while the address-of operator retrieves the location.QUESTION 5
Which statement about references is FALSE?
A reference is not an object.
A reference can be null.
A reference must be initialized.
A reference cannot be redirected to a new object.
✅ Correct!
References must always be bound to a valid object; there is no such thing as a 'null reference'.❌ Incorrect
References are fixed aliases and do not have a null state, unlike pointers.Module Assessment: Pointers, References, and Sales_data
Applying Compound Types and Structs
You are tasked with upgrading a legacy system. You must manage data using custom structures and ensure memory safety through correct pointer and reference usage.
Q
Exercise 2.41: Based on the Sales_data struct (including bookNo, units_sold, and revenue), how would you rewrite a loop to read multiple transactions and update the total? Focus on the main function logic.
Solution:
To implement Exercise 2.41, define the Sales_data struct at the top of the file. In main(), declare a 'total' object. Use a while loop with 'std::cin >> total.bookNo >> total.units_sold >> price' where revenue is calculated as 'units_sold * price'. For subsequent entries, check if 'trans.bookNo == total.bookNo'. If true, update total.units_sold and total.revenue; if false, print the current total and reset it to the new transaction.
To implement Exercise 2.41, define the Sales_data struct at the top of the file. In main(), declare a 'total' object. Use a while loop with 'std::cin >> total.bookNo >> total.units_sold >> price' where revenue is calculated as 'units_sold * price'. For subsequent entries, check if 'trans.bookNo == total.bookNo'. If true, update total.units_sold and total.revenue; if false, print the current total and reset it to the new transaction.
Q
Consider the following snippet: 'int i = 42; int *p = &i; *p = *p * *p;'. Explain the final value of 'i' and the role of the asterisk in each part of the second line.
Solution:
The final value of 'i' is 1764 (42 squared). In '*p = *p * *p;', the first and second asterisks are dereference operators used to fetch and store values at the address held by p. The third asterisk is the multiplication operator. This demonstrates how pointers allow indirect modification of the original variable 'i'.
The final value of 'i' is 1764 (42 squared). In '*p = *p * *p;', the first and second asterisks are dereference operators used to fetch and store values at the address held by p. The third asterisk is the multiplication operator. This demonstrates how pointers allow indirect modification of the original variable 'i'.